package com.example.demo.controller;

import com.example.demo.model.PunchRecord;
import com.example.demo.model.ResultMsg;
import com.example.demo.service.PunchRecordService;
import com.example.demo.test.test;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/v1/punch")
public class PunchCardController {

    @Resource
    private PunchRecordService punchCardService;

    //获取企业授权码
    @GetMapping("/getAuthCode")
    public String getAuthCode(@RequestParam("companyCode") String companyCode) {
        log.info("获取企业授权码：{}", companyCode);
        String authcode = punchCardService.getAuthCode(companyCode);

        if (authcode != null) {
            return "{\"code\":0,\"message\":\"success\",\"企业授权码为\":\"" + authcode + "\"}";
        } else {
            return "{\"code\":1,\"message\":\"打卡机厂商编码无效\"}";
        }
    }

    //处理打卡机打卡数据
    @PostMapping("/record")
    public ResultMsg receivePunchRecord(@RequestBody PunchRecord record) {
        log.info("处理打卡机打卡数据：{}", record);
        ResultMsg resultMsg = new ResultMsg();
        try {
            punchCardService.processPunchRecord(record);

        //    test.timeTask();
            resultMsg.setSuccess(true);
            resultMsg.setCode("0");
        } catch (Exception e) {
            resultMsg.setSuccess(false);
            resultMsg.setCode("1");
        }
        return resultMsg;
    }
}

